package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import control.SortWordsApp;


/**
 * MyPanel cria  painel com meus botoes.
 */
public class MyPanel extends JPanel
					 implements ActionListener { 

	private static final long serialVersionUID = 1L;
	protected JButton   b1, b2, b3, b4, b5, b6, b7, b8, b9;

	//  Labels to identify the fields
	//Create the text fields and set them up.
	//Strings for the labels
	private JLabel findLabel;
	private static String findString = "Achar:";

	//Fields for data entry
	private JTextField findField;
	BuscaPeUI		busca;
	
	
	
	public MyPanel(BuscaPeUI bp) {		
		System.out.println("Inicio do MyPanel ");
		
		busca=bp; 
		
		// Add buttons.
		JButton b1=newButton("Resumo", Color.white, "abstract");
		JButton b2=newButton("Reivindicacoes", Color.white, "claims");
		JButton b3=newButton("Descricao", Color.white, "description");
		JButton b4=newButton("Completo", Color.white, "specification");
		JButton b5=newButton("Familia", Color.white, "family");      
		JButton b6=newButton("Contar", Color.white, "contar");
		JButton b7=newButton("Selecionar", Color.white, "select");
		JButton b_ant=new JButton(new ImageIcon("left.gif"));
		b_ant.setActionCommand("anterior");
		b_ant.addActionListener(this);
		//JButton b8=newButton(rightarrow, Color.magenta,"proximo" );
		JButton b8=new JButton(new ImageIcon("right.gif"));
		b8.setActionCommand("proximo");
		b8.addActionListener(this);
		//        JButton b9=newButton("Limpar", Color.white,"limpar" );
		JButton b9=new JButton("Limpar");
		b9.setActionCommand("limpar");
		b9.addActionListener(this);
		b9.setBackground(Color.white);

		//Create the label.
		findLabel = new JLabel(findString);

		//Create the text fields and set them up.
		findField = new JTextField(7);
		findField.setActionCommand("Achar");
		findField.addActionListener(this);
		//Tell accessibility tools about label/textfield pairs.
		findLabel.setForeground(Color.white);
		findLabel.setLabelFor(findField);

		//Add Components to this container, using the default FlowLayout.
		add(b1);
		add(b2);
		add(b3);
		add(b4);
		add(b5);
		add(b6);
		add(b7); 
		add(b_ant);
		add(b8);
		add(findLabel);
		add(findField);
		add(b9);
		this.setBackground(Color.black);
		this.setFont(new Font("sansserif", Font.BOLD, 14));


		System.out.println("Fim do MyPanel ");
	}
	
	
	public JButton newButton(String label, Color color, String actionCommand){

		System.out.println("Inicio do MyPanel -> newButton ");
		
		JButton bt = new JButton(label);
		bt.setFont(new Font("sansserif", Font.BOLD, 10));
		bt.setBackground(color);
		bt.setActionCommand(actionCommand);
		bt.addActionListener(this);
		
		System.out.println("Fim do MyPanel -> newButton ");
		return bt;
		
	}
	public void actionPerformed(ActionEvent e) {

		System.out.println("Inicio do MyPanel -> actionPerformed ");
		Icon icon = new ImageIcon(busca.patente.getFigura()); 
		if ("abstract".equals(e.getActionCommand())) {
			System.out.println(busca.patente.getTitle());
			busca.textPane.sendSelecTextToPane("");
			busca.textPane.sendSelecTextToPane(busca.patente.getAbsTract());
			busca.textPane.insertIcon(icon);
		} 
		if ("claims".equals(e.getActionCommand())) {
			busca.textPane.sendSelecTextToPane("");
			busca.textPane.sendSelecTextToPane(busca.patente.getClaims());
			busca.textPane.insertIcon(icon);
		} 
		if ("description".equals(e.getActionCommand())) {
			busca.textPane.sendSelecTextToPane(busca.patente.getDescription());
		} 
		if ("specification".equals(e.getActionCommand())) {
			busca.textPane.sendSelecTextToPane(busca.patente.getTitle()+busca.patente.getAbsTract()+busca.patente.getClaims()+busca.patente.getDescription());
		} 
		if ("family".equals(e.getActionCommand())) {
			busca.textPane.sendSelecTextToPane(busca.patente.getFamily()+busca.patente.getCiting());
		} 
		if ("contar".equals(e.getActionCommand())) { 
			String txt=busca.textPane.getText();
			busca.textPane.setText(new SortWordsApp (txt).texto); 
		} 
		if ("select".equals(e.getActionCommand())) {
			busca.textPane.selectWords();
		} 
		if ("anterior".equals(e.getActionCommand())) {
			busca.textPane.previousWord();
		}  
		if ("proximo".equals(e.getActionCommand())) {
			busca.textPane.nextWord();
		}  
		if ("Achar".equals(e.getActionCommand())) {      	
			busca.splitWord=findField.getText();
			busca.textPane.findWord(busca.splitWord);
		}  
		if ("limpar".equals(e.getActionCommand())) {
			busca.textPane.selectAll();
			busca.textPane.replaceSelection ("");
		} 

		System.out.println("Fim do MyPanel -> actionPerformed ");

	}


}
